#ifndef MY_RENDER_SCREEN
#define MY_RENDER_SCREEN

#include "../config.hpp"
#include "./pixel.hpp"
#include<iostream>
using std::cout;


#include <vector>
using namespace std;

namespace drawer{

    class Screen {
        public:
            int width = windowWidth;
            int height = windowHeight;
        
            // 改成二维数组,这里是一个很深刻的教训，一定不能使用vector，不然性能损失超级大，根本用不了。
            Pixel** content;
            // 复制构造函数，涉及到深拷贝
            Screen& operator=(const Screen& oldScreen){
                content=new Pixel*[width + 1];
                for (int i = 0; i <= width; ++i) {
                    content[i] = new Pixel[height + 1];
                }
                for (int x = 0; x <= width; x++) {
                    for (int y = 0; y <= height; y++) {
                        content[x][y].x = x;
                        content[x][y].y = y;
                        content[x][y].red = oldScreen.content[x][y].red;
                        content[x][y].green = oldScreen.content[x][y].green;
                        content[x][y].blue = oldScreen.content[x][y].blue;
                    }
                }
            }
            // 复制赋值函数也要，以防万一
            Screen(const Screen& oldScreen){
                content=new Pixel*[width + 1];
                for (int i = 0; i <= width; ++i) {
                    content[i] = new Pixel[height + 1];
                }
                for (int x = 0; x <= width; x++) {
                    for (int y = 0; y <= height; y++) {
                        content[x][y].x = x;
                        content[x][y].y = y;
                        content[x][y].red = oldScreen.content[x][y].red;
                        content[x][y].green = oldScreen.content[x][y].green;
                        content[x][y].blue = oldScreen.content[x][y].blue;
                    }
                }
            }
        
            // 生成一个白色的屏幕
            Screen() {
                // 分配内存
                // width+1是因为屏幕的范围是[0,width]，总共width+1个点
                content = new Pixel*[width + 1];
                for (int i = 0; i <= width; ++i) {
                    content[i] = new Pixel[height + 1];
                }
        
                for (int x = 0; x <= width; x++) {
                    for (int y = 0; y <= height; y++) {
                        content[x][y].x = x;
                        content[x][y].y = y;
                        content[x][y].red = defaultBackgroundRed;
                        content[x][y].green = defaultBackgroundGreen;
                        content[x][y].blue = defaultBackgroundBlue;
                    }
                }
            }
            Screen(int width,int height):width(width),height(height) {
                // 分配内存
                // width+1是因为屏幕的范围是[0,width]，总共width+1个点
                content = new Pixel*[width + 1];
                for (int i = 0; i <= width; ++i) {
                    content[i] = new Pixel[height + 1];
                }
        
                for (int x = 0; x <= width; x++) {
                    for (int y = 0; y <= height; y++) {
                        content[x][y].x = x;
                        content[x][y].y = y;
                        content[x][y].red = defaultBackgroundRed;
                        content[x][y].green = defaultBackgroundGreen;
                        content[x][y].blue = defaultBackgroundBlue;
                    }
                }
            }
        
            // 更新屏幕中的某个点的颜色
            void updateScreenPixel(int x, int y, GLfloat red, GLfloat green, GLfloat blue) {
                content[x][y].red = red;
                content[x][y].green = green;
                content[x][y].blue = blue;
            }
        
            // 删除屏幕
            void clearScreen() {
                for (int i = 0; i <= width; ++i) {
                    delete[] content[i];
                }
                delete[] content;
                content = nullptr;
            }
        
            // 析构函数
            ~Screen() {
                clearScreen();
            }
        };

}



#endif